package xiao_jin.fantastic_journey.api.capability;

import com.google.common.collect.Lists;
import net.minecraft.util.Identifier;
import xiao_jin.fantastic_journey.FantasticJourney;

import java.util.List;

@SuppressWarnings("unused")
public final class CapabilityManager {
    private CapabilityManager() {
    }

    public static final CapabilityManager INSTANCE = new CapabilityManager();
    private final List<Capability> data = Lists.newArrayList();

    public static CapabilityManager manager() {
        return INSTANCE;
    }

    public <T extends Capability> T registry(T capability) {
        var dataSame = data.stream().filter(it -> it.getID().equals(capability.getID())).findFirst();
        if (dataSame.isPresent())
            throw new RuntimeException("A capability is registry twice: " + capability.getID().toString());

        data.add(capability);
        FantasticJourney.LOGGER.info("Capability {} registry!", capability.getID().toString());
        return capability;
    }

    public Capability findCapability(Identifier id) {
        return CapabilityManager.manager().data.stream().filter(cp -> cp.getID().equals(id)).findFirst().orElse(null);
    }

    public List<Capability> copyData() {
        return Lists.newArrayList(data);
    }
}
